from fastapi import APIRouter, Security, Depends
from fastapi_pagination.iterables import Page
from redis import Redis

from api.v1.admin_user.schemas import CreateRole, Register, UserList, UserInfo, UpdateUserInfo, Permission, \
    RoleList, PermissionList
from api.v1.admin_user.services import create_role, create_user, delete_role, user_list, user_info, delete_user, \
    update_user_info, create_permissions, roles_join_permissions, role_list, permissions_list, delete_permissions, \
    reset_password, redis_test_demo
from sql_app.redisServe import get_redis
from utils.authorization import auth
from utils.scopes import Scopes, set_scopes

admin = APIRouter(tags=["admin-user"], prefix="/admin")


@admin.post("/register", summary="用户注册",
            dependencies=[Security(auth, scopes=set_scopes(Scopes(roles=["admin"], permissions=["1"])))])
async def register_user(register: Register):
    return await create_user(register)


@admin.post("/create_role", summary="创建角色",
            dependencies=[
                Security(auth, scopes=set_scopes(Scopes(roles=["admin"], permissions=["1"])))
            ])
async def role_create(createRole: CreateRole):
    """
    创建角色
    :param createRole:
    :return:
    """
    return await create_role(createRole)


@admin.delete("/delete_role", summary="删除角色",
              dependencies=[Security(auth, scopes=set_scopes(Scopes(roles=["admin"], permissions=["1"])))])
async def role_delete(id: int):
    """
    删除角色
    :param id:
    :return:
    """
    return await delete_role(id)


@admin.get(
    "/user_list",
    summary="查询用户列表",
    response_model=Page[UserList],
    dependencies=[
        Security(auth, scopes=set_scopes(Scopes(roles=["admin"], permissions=["1"])))
    ],
)
async def get_user_list(
        username: str = None,
        start_time: str = None,
        end_time: str = None,
        is_frozen: bool = None,
        is_admin: bool = None,
):
    users = await user_list(username, start_time, end_time, is_frozen, is_admin)
    return users


@admin.get(
    "/user_info/{id}",
    summary="查询用户详细信息",
    response_model=UserInfo,
    dependencies=[
        Security(auth, scopes=set_scopes(Scopes(roles=["admin"], permissions=["1"])))
    ],
)
async def get_user_info(id: int):
    return await user_info(id)


@admin.delete(
    "/user/{id}",
    summary="删除用户",
    dependencies=[
        Security(auth, scopes=set_scopes(Scopes(roles=["admin"], permissions=["1"])))
    ],
)
async def delete_users(id: int):
    return await delete_user(id)


@admin.put(
    "/update_user_info/{id}",
    summary="修改用户信息",
    dependencies=[
        Security(auth, scopes=set_scopes(Scopes(roles=["admin"], permissions=["1"])))
    ],
)
async def update_users_info(id: int, user_info: UpdateUserInfo):
    """
    更新用户信息
    :param id:
    :param user_info:
    :return:
    """
    return await update_user_info(id, user_info)


@admin.post("/reset_password", summary="重置用户密码",
            dependencies=[Security(auth, scopes=set_scopes(Scopes(roles=["admin"], permissions=["1"])))])
async def admin_user_reset_password(user_id: str):
    """
    重置用户密码
    :param user_id:
    :return:
    """
    return await reset_password(user_id)


@admin.post(
    "/create_permissions",
    summary="创建权限",
    dependencies=[
        Security(auth, scopes=set_scopes(Scopes(roles=["admin"], permissions=["1"])))
    ],
)
async def create_permission(permissions: Permission):
    return await create_permissions(permissions)


@admin.post(
    "/role_{role_id}/permissions_{permissions_id}",
    summary="角色分配权限",
    dependencies=[
        Security(auth, scopes=set_scopes(Scopes(roles=["admin"], permissions=["1"])))
    ],
)
async def role_join_permissions(role_id: int, permissions_id: int):
    return await roles_join_permissions(role_id, permissions_id)


@admin.get(
    "/role_list",
    summary="获取角色列表",
    response_model=Page[RoleList],
    dependencies=[
        Security(auth, scopes=set_scopes(Scopes(roles=["admin"], permissions=["1"])))
    ],
)
async def roles_list(name: str = None):
    return await role_list(name)


@admin.get(
    "/permissions",
    summary="获取权限列表",
    response_model=Page[PermissionList],
    dependencies=[
        Security(auth, scopes=set_scopes(Scopes(roles=["admin"], permissions=["1"])))
    ],
)
async def get_permissions_list(description: str = None, code: int = None):
    return await permissions_list(description, code)


@admin.delete(
    "/permissions/{id}",
    summary="删除权限",
    dependencies=[
        Security(auth, scopes=set_scopes(Scopes(roles=["admin"], permissions=["1"])))
    ],
)
async def del_permissions(id: int):
    return await delete_permissions(id)


@admin.get("/test_redis", summary="测试rides连接池", dependencies=[
    Security(auth, scopes=set_scopes(Scopes(roles=["admin"], permissions=["1"])))
], )
async def redis_demo(key: str, value: str, redis: Redis = Depends(get_redis)):
    return await redis_test_demo(key, value, redis)
